﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_SiphonLife : TargetSpell
    {
        // Level 1
        private int BaseDamageLowRank1 = 80;
        private int BaseDamageHighRank1 = 100;

        // Level 9
        private int BaseDamageLowRank2 = 130;
        private int BaseDamageHighRank2 = 160;


        //Mentality Drain Magic 
        //Level 25
        private int MentBaseDamageLowRank1 = 200;
        private int MentBaseDamageHighRank1 = 250;

        private int getBaseDamageLow(SpellIcon _spellIcon)
        {
            if (_spellIcon.Skill == "Unholy")
            {
                if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                    return BaseDamageLowRank2;
                else
                    return BaseDamageLowRank1;
            }
            else if (_spellIcon.Skill == "Mentality")
                return MentBaseDamageLowRank1;
            else
                return 1;
        }

        private int getBaseDamageHigh(SpellIcon _spellIcon)
        {
            if (_spellIcon.Skill == "Unholy")
            {
                if (_spellIcon.Name.IndexOf("Rank 2") > -1)
                    return BaseDamageHighRank2;
                else
                    return BaseDamageHighRank1;
            }
            else if (spellIcon.Skill == "Mentality")
                return MentBaseDamageHighRank1;
            else
                return 2;
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;



            List<Character> CharactersInUpdateRange = new List<Character>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharactersInUpdateRange.Add((Character)creaturebase);
                }
            }

            int DamageAmount = myCreatureBase.GameHandler.random.Next(getBaseDamageLow(this.spellIcon), getBaseDamageHigh(this.spellIcon) + 1);
            DamageAmount = SkillManager.AdjustForCombat(CastLevel, DamageAmount, myCreatureBase.IntelligenceTotal);

            int DamageResult = TargetCreatureBase.CalculateGetSpellDamagePerResists(DamageAmount, spellIcon.Skill);

            if (spellIcon.Skill == "Unholy")
            {
                int absorbedDamage;
                TargetCreatureBase.InflictSpellDamage(DamageResult, CastingCharacterID, CastingCreatureGUID, spellIcon.Skill, out absorbedDamage);

                DamageResult = DamageResult - absorbedDamage;

                myCreatureBase.HP = myCreatureBase.HP + DamageResult;

                TargetCreatureBase.Update_Character();
                myCreatureBase.Update_Character();

                if (myCreatureBase is Character)
                {
                    Character myCharacter = (Character)myCreatureBase;

                    Message healMsg1 = new Message();
                    healMsg1.CreatureID = myCharacter.CreatureID;
                    healMsg1.Type = "damage";
                    healMsg1.Text = "You steal HP from " + TargetCreatureBase.Name + "! (" + DamageResult.ToString() + ")";

                    if (absorbedDamage != 0)
                        healMsg1.Text = healMsg1.Text + " ABSORBED " + absorbedDamage.ToString();

                    myCharacter.AddMessage(healMsg1);
                }
                if (TargetCreatureBase is Character)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;

                    Message healMsg1 = new Message();
                    healMsg1.CreatureID = targetCharacter.CreatureID;
                    healMsg1.Type = "damaged";
                    healMsg1.Text = "Your HP have been stolen by " + myCreatureBase.Name + "! (" + DamageResult.ToString() + ")";

                    if (absorbedDamage != 0)
                        healMsg1.Text = healMsg1.Text + " ABSORBED " + absorbedDamage.ToString();

                    targetCharacter.AddMessage(healMsg1);
                }
            }
            else if (spellIcon.Skill == "Mentality")
            {
                if (TargetCreatureBase.Mana < DamageResult)
                    DamageResult = TargetCreatureBase.Mana;

                TargetCreatureBase.Mana = TargetCreatureBase.Mana - DamageResult;
                myCreatureBase.Mana = myCreatureBase.Mana + DamageResult;

                TargetCreatureBase.Update_Character();
                myCreatureBase.Update_Character();

                if (myCreatureBase is Character)
                {
                    Character myCharacter = (Character)myCreatureBase;

                    Message healMsg1 = new Message();
                    healMsg1.CreatureID = myCharacter.CreatureID;
                    healMsg1.Type = "damage";
                    healMsg1.Text = "You steal Mana from " + TargetCreatureBase.Name + "! (" + DamageResult.ToString() + ")";

                    myCharacter.AddMessage(healMsg1);
                }
                if (TargetCreatureBase is Character)
                {
                    Character targetCharacter = (Character)TargetCreatureBase;

                    Message healMsg1 = new Message();
                    healMsg1.CreatureID = targetCharacter.CreatureID;
                    healMsg1.Type = "damaged";
                    healMsg1.Text = "Your Mana has been stolen by " + myCreatureBase.Name + "! (" + DamageResult.ToString() + ")";

                    targetCharacter.AddMessage(healMsg1);
                }
            }

            List<Character> CharsToUpdate = new List<Character>();
            MapItem mapitemToUpdate = TargetCreatureBase.currentMapItem;
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if ((Math.Abs(mapitem.MapX - myCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - myCreatureBase.MapY) <= 4) ||
                    (Math.Abs(mapitem.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(mapitem.MapY - TargetCreatureBase.MapY) <= 4))
                {
                    foreach (CreatureBase cb in mapitem.CreaturesBases)
                    {
                        if (cb is Character)
                            CharsToUpdate.Add((Character)cb);
                    }
                }
                if (TargetCreatureBase.HP < 1 && mapitem.MapX == TargetCreatureBase.MapX && mapitem.MapY == TargetCreatureBase.MapY)
                {
                    if (!(TargetCreatureBase is Character))
                    {
                        mapitem.CreaturesBases.Remove(TargetCreatureBase);
                    }
                    mapitemToUpdate = mapitem;
                }
            }
            foreach (Character character in CharsToUpdate)
            {
                if (TargetCreatureBase.HP < 1)
                    character.AddMapItem(mapitemToUpdate);
                else if (spellIcon.Skill == "Unholy")
                    character.UpdateCreatureBaseHp(TargetCreatureBase);
                else if (spellIcon.Skill == "Mentality")
                    character.UpdateCreatureBaseMana(TargetCreatureBase);

                character.PlaySound("SiphonLife", TargetCreatureBase.MapX, TargetCreatureBase.MapY);
            }
            
        }
    }
}
